package com.tuliomoura.got.guide;

import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.SpinnerAdapter;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.tuliomoura.got.guide.globals.FontsEnum;

public class MainActivity extends SherlockFragmentActivity
{
	public static final int HOUSES_FRAGMENT_POSITION = 0;
	public static final int CHARACTERS_FRAGMENT_POSITION = 1;
	public static final int MAPS_FRAGMENT_POSITION = 2;
	
	private RelativeLayout mFragmentContainer;
	private SpinnerAdapter mSpinnerAdapter;
	private OnNavigationListener mNavigationListener;
	private SherlockFragment[] mFragments;
	private String[] mFragmentsNames;
	private int mCurrentFragment;
	private ActionBar mActionBar;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//se savedInstanceState não é null, tudo já deve estar setado
		if (savedInstanceState != null)
			return;
		//instancia os fragmentos usados
		createFragments();
		//seta o adapter para preencher o spinner de fragmentos
		setSpinnerAdapter();
		//seta o listener para navegação entre fragmentos
		createNavigationListener();
		//seta o layout principal como sendo o do fragment_houses
		mFragmentContainer = (RelativeLayout) findViewById(R.id.activity_guide_fragment_container);
		getSupportFragmentManager().beginTransaction().add(mFragmentContainer.getId(), mFragments[HOUSES_FRAGMENT_POSITION]).commit();
		mCurrentFragment = HOUSES_FRAGMENT_POSITION;
		
		mActionBar = getSupportActionBar();
		mActionBar.setDisplayShowTitleEnabled(false);
		mActionBar.setDisplayHomeAsUpEnabled(false);
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		mActionBar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationListener);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater menuInflater = getSupportMenuInflater();
		menuInflater.inflate(R.menu.fragment_houses_menu, menu);
	    return true;
	}
	
	private void createFragments()
	{
		if (mFragments == null)
		{
			HousesFragment houses = new HousesFragment();
			CharactersFragment characters = new CharactersFragment();
			MapsFragment maps = new MapsFragment();
			mFragments = new SherlockFragment[3];
			mFragments[HOUSES_FRAGMENT_POSITION] = houses;
			mFragments[CHARACTERS_FRAGMENT_POSITION] = characters;
			mFragments[MAPS_FRAGMENT_POSITION] = maps;
			
			mFragmentsNames = getResources().getStringArray(R.array.fragments_names);
		}
	}
	
	private void setSpinnerAdapter()
	{
		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.sherlock_spinner_item);
		adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
		
		for (int i = 0; i < mFragmentsNames.length; i++)
			adapter.add(mFragmentsNames[i]);
		mSpinnerAdapter = adapter;
		//ArrayAdapter.createFromResource(this, R.array.fragments_names, R.layout.sherlock_spinner_dropdown_item);
	}
	
	private void createNavigationListener()
	{
		if (mNavigationListener == null)
		{
			mNavigationListener = new OnNavigationListener()
			{
				public boolean onNavigationItemSelected(int itemPosition, long itemId) 
				{
					if (itemPosition != mCurrentFragment)
						replaceFragment(itemPosition);
					return true;
				}
			};
		}
	}
	
	private void replaceFragment(int fragmentId)
	{
		final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(mFragmentContainer.getId(), mFragments[fragmentId]);
		transaction.commit();
		mCurrentFragment = fragmentId;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
	    switch (item.getItemId())
	    {
	        case R.id.font_aniron:
	            setPreferredFont(FontsEnum.ANIRON);
	            break;
	        case R.id.font_black_knight:
	            setPreferredFont(FontsEnum.BLACK_KNIGHT);
	            break;
	        case R.id.font_crusades:
	        	setPreferredFont(FontsEnum.CRUSADES);
	        	break;
	        case R.id.font_english_towne:
	        	setPreferredFont(FontsEnum.ENGLISH_TOWNE);
	        	break;
	        case R.id.font_frederick_text:
	        	setPreferredFont(FontsEnum.FREDERICK_TEXT);
	        	break;
	        case R.id.font_lohengrin:
	        	setPreferredFont(FontsEnum.LOHENGRIN);
	        	break;
	        case R.id.font_old_london:
	        	setPreferredFont(FontsEnum.OLD_LONDON);
	        	break;
	        case R.id.font_prince_valiant:
	        	setPreferredFont(FontsEnum.PRINCE_VALIANT);
	        	break;
	        case R.id.font_west_minster:
	        	setPreferredFont(FontsEnum.WEST_MINSTER);
	        	break;
	    }
    	return true;
	}
	
	private void setPreferredFont(FontsEnum font)
	{
		if (font != null)
		{
			final Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
			editor.putString(GuideApplication.PREFERRED_FONT, font.name());
			editor.commit();
		}
	}
	
}
